<?php
namespace Admin\Controller;
use Think\AjaxPage;
use Think\Controller;

class AuthorController extends CommonController
{
    public function _initialize()
    {
        // 数组存放不需添加进权限控制的方法
        $params = array();
        parent::_initialize($params);
    }

    // 授权列表
    public function index()
    {
        $this->display();
    }

    // AJAX 读列表
    public function ajax_auths_list()
    {
        $where = ' 1 = 1 '; // 搜索条件
        // 关键词搜索
        $key = I('key') ? trim(I('key')) : '';
        if($key && $key !== '') {
            $where = "$where AND (auth_code = '$key' or company LIKE '%$key%' or url = '$key')" ;
        }
        $count = M('auth')->where($where)->count();
        $Page  = new AjaxPage($count, C('PAGE_NUM'));
        $show = $Page->show();
        $order_str = "`{$_POST['orderby1']}` {$_POST['orderby2']}";
        $authors = M('auth')->where($where)->order($order_str)->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('auths',$authors);
        $this->assign('page',$show);// 赋值分页输出
        $this->display();
    }

    // 添加授权
    public function new_author()
    {
        if(IS_POST)
        {
            $company = trim(I("post.company"));
            $ip = trim(I("post.ip"));
            $url = trim(I("post.url"));
            $auth_salt = trim(I("post.auth_salt"));
            $use_end = trim(I("post.use_end"));
            $forever = to_int(I("post.forever"));
            if($company == '' || $ip == '' || $url == ''){
                $this->error("信息填写不完整" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            $isset_auth = M("auth")->where("url = '%s'",array($url))->find();
            if($isset_auth){
                $this->error("已添加此授权" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            if($use_end == '' && $forever != 1){
                $this->error("到期时间选择有误" ,$_SERVER['HTTP_REFERER'] ,1);
            }
            if($auth_salt == ''){
                $auth_salt = substr(md5(time().$ip.$url) ,-10);
            }
            $spacing_time = date("Y-m-d");
            if($use_end != ''){
                $spacing_time = date('Y-m-d' ,strtotime($use_end));
            }elseif($forever == 1){
                $spacing_time = 8888;
            }
            $insertData['company'] = $company;
            $insertData['ip'] = $ip;
            $insertData['url'] = $url;
            $insertData['auth_salt'] = $auth_salt;
            $insertData['use_end'] = $spacing_time;
            $insertData['auth_code'] = auth_code_encode($ip ,$url ,$auth_salt);
            $rs = M("auth")->add($insertData);
            if($rs !== false){
                $this->success("添加成功" ,U("Author/index") ,1);
            }else{
                $this->error("添加失败" ,$_SERVER['HTTP_REFERER'] ,1);
            }
        }else{
            $this->display();
        }
    }

    // 删除授权
    public function del_auth()
    {
        if(IS_AJAX)
        {
            $auth_id = to_int(I("post.id"));
            if($auth_id == 0){
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '参数错误'));
            }
            $rs = M("auth")->where("auth_id = %d" ,array($auth_id))->delete();
            if($rs !== false){
                $this->ajaxReturn(array("status" => 'success'));
            }else{
                $this->ajaxReturn(array("status" => 'error' ,'msg' => '删除失败'));
            }
        }else{
            exit("error");
        }
    }

    public function download_auth()
    {
        $auth_id = to_int(I("get.id"));
        $auth = M("auth")->where("auth_id = %d" ,array($auth_id))->find();
        if(!$auth){
            $this->error("参数有误" ,U('Author/index') ,1);
        }
        $auth_dir = "./Data/auth_files/";
        if(!dir($auth_dir)){
            mkdir($auth_dir ,0777,false);
        }
        $auth_file = $auth_dir."auth_lock_" .$auth_id.".php";
        @unlink($auth_file);
        $content = "<?php return array('auth_code' => '". $auth['auth_code'].
            "','auth_salt' => '".$auth['auth_salt'].
            "','use_end' => '".$auth['use_end'].
            "','ip' => '".$auth['ip'].
            "' ,'url' => '".$auth['url']."'); ?>";
        file_put_contents($auth_file ,$content);
        ob_start();
        $name = "auth_lock.php";
        header( "Content-type:application/octet-stream ");
        header( "Accept-Ranges:bytes ");
        header( "Content-Disposition:attachment;filename={$name}");
        $size = readfile($auth_file);
        header( "Accept-Length: " .$size);
    }
}